package cn.ycoz.cloud.windspeed.concurrent;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class ObserverAdaptor<T> implements Observer<T> {

    protected Disposable disposable;
    private CountDownLatch countDownLatch = new CountDownLatch(1);
    private final CompositeDisposable compositeDisposable = new CompositeDisposable();

    public ObserverAdaptor() {
    }

    public ObserverAdaptor(int count) {
        countDownLatch = new CountDownLatch(count);
    }

    @Override
    public void onSubscribe(Disposable d) {
        this.disposable = d;
        compositeDisposable.add(d);
    }

    @Override
    public void onNext(T t) {

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public void onComplete() {

    }

    public void countDown() {
        countDownLatch.countDown();
    }

    public void destroy() {
        compositeDisposable.clear();
    }

    public CountDownLatch getCountDownLatch() {
        return countDownLatch;
    }
}
